﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Engine.Graphics;

namespace Engine
{
    public enum Brush { Circle, Square }
    public static class TextureUtils
    {

        public static Texture2D createBlankTexture(int sizeX, int sizeY, Color color)
        {
            Texture2D tex = new Texture2D(GraphicsEngine.Instance.GraphicsDevice, sizeX, sizeY);

            Color[] c = new Color[sizeX * sizeY];
            for (int i = 0; i < sizeX * sizeY; i++)
            {
                c[i] = color;
            }
            tex.SetData<Color>(c);
            return tex;
        }

        public static void SetPixel(int x, int y, Texture2D tex, Color c)
        {
            while (x > tex.Width) x -= tex.Width;
            while (y > tex.Height) y -= tex.Height;
            Color[] data = new Color[tex.Width * tex.Height];
            tex.GetData<Color>(data);
            data[y * tex.Width + x] = c;
            tex.SetData<Color>(data);
        }

        public static Color[] SetPixel(int x, int y, Color[] colors, Color c, int _max)
        {
            while (x >= _max) x -= _max;
            while (y >= _max) y -= _max;
            Color[] data = colors;
            data[y * _max + x] = c;
            return data;
        }

        public static Color GetPixel(Texture2D tex, int x, int y)
        {
            while (x >= 250) x -= 250;
            while (y >= 250) y -= 250;
            Color[] c = new Color[tex.Width * tex.Height];
            tex.GetData<Color>(c);
            return c[y * tex.Width + x];
        }

        public static Color GetPixel(Color[] tex, int x, int y)
        {
            while (x >= 250) x -= 250;
            while (y >= 250) y -= 250;
            return tex[y * (int)Math.Sqrt(tex.Length) + x];
        }

        public static void FloodFill(Color[] tex, int x, int y, Color rColor)
        {
            while (x >= 250) x -= 250;
            while (y >= 250) y -= 250;
            Color tColor = GetPixel(tex, x, y);
            SetPixel(x, y, tex, rColor, 250);

            FloodFill(tex, x + 1, y, tColor, rColor);
            FloodFill(tex, x - 1, y, tColor, rColor);
            FloodFill(tex, x, y - 1, tColor, rColor);
            FloodFill(tex, x, y + 1, tColor, rColor);
        }
        private static void FloodFill(Color[] tex, int x, int y, Color tColor, Color rColor)
        {

            if (x < 0 || y < 0 || x >= 250 || y >= 250)
            {
                return;
            }
            if (!GetPixel(tex, x, y).Equals(tColor))
            {
                return;
            }

            SetPixel(x, y, tex, rColor, 250);

            FloodFill(tex, x + 1, y, tColor, rColor);
            FloodFill(tex, x - 1, y, tColor, rColor);
            FloodFill(tex, x, y - 1, tColor, rColor);
            FloodFill(tex, x, y + 1, tColor, rColor);
        }
    }
}
